package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.db.HomeRecommendationEntity;
import com.lanou.connotationjoke.db.HomeSubscriptionEntity;
import com.lanou.connotationjoke.db.HomeSubscriptionEntityDao;
import com.lanou.connotationjoke.entity.CategoryList;
import com.lanou.connotationjoke.entity.DiscoveryEty;
import com.lanou.connotationjoke.tool.DaoSingleton;
import com.lanou.connotationjoke.tool.MyAppllication;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/21.
 */
public class DiscoveryHotListViewAdaper extends BaseAdapter{
    private Context context;
    private List<CategoryList> categoryLists;
    private HomeSubscriptionEntityDao entityDao
            = DaoSingleton.getInstance().getHomeSubscriptionEntityDao();

    public DiscoveryHotListViewAdaper(Context context, DiscoveryEty discoveryEty) {
        this.context = context;
        List<CategoryList> ss = discoveryEty.getData().getCategories().getCategory_list();
        List<HomeSubscriptionEntity> qq = entityDao.queryBuilder().list();
        Log.d("数据库",qq.size()+"");
        categoryLists = new ArrayList<>();
        for (CategoryList categoryList : ss){
            boolean different = true;
            for (HomeSubscriptionEntity homeSubscriptionEntity : qq){
                if (categoryList.getName().equals(homeSubscriptionEntity.getName())){
                    different = false;
                }
            }
            if (different){
                categoryLists.add(categoryList);
            }
        }

    }

    @Override
    public int getCount() {
        return categoryLists.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_listview_discovery_hot,null);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        holder.imageView.setErrorImageResId(R.mipmap.ic_launcher);
        holder.imageView.setImageUrl(categoryLists.get(position).getSmall_icon_url()
                , VolleySingleton.getImageLoader());
        holder.nameTv.setText(categoryLists.get(position).getName());
        holder.introTv.setText(categoryLists.get(position).getIntro());
        holder.subscribeCountTv.setText("" + categoryLists.get(position).getSubscribe_count());
        holder.totalUpdatesTv.setText("" + categoryLists.get(position).getTotal_updates());
        holder.layoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyAppllication.hasNetwork()){
                    WebViewActivity.goToWebViewActivity(context,categoryLists
                            .get(position).getShare_url());
                }else {
                    Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                }
            }
        });
        holder.subscriptionTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "已收藏", Toast.LENGTH_SHORT).show();
                long i = entityDao.count();
                CategoryList categoryList = categoryLists.get(position);
                entityDao.insert(new HomeSubscriptionEntity(i,categoryList.getSmall_icon_url()
                ,categoryList.getName(),categoryList.getIntro(),categoryList.getShare_url()));
                EventBus.getDefault().post("refresh");
                categoryLists.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }


    class MyViewHolder{
        NetworkImageView imageView;
        TextView nameTv;
        TextView introTv;
        TextView subscribeCountTv;
        TextView totalUpdatesTv;
        View layoutView;
        TextView subscriptionTv;

        public MyViewHolder(View itemView){
            imageView = (NetworkImageView) itemView.findViewById(R.id.iv_discovery_hot);
            nameTv = (TextView) itemView.findViewById(R.id.tv_discovery_hot_name);
            introTv = (TextView) itemView.findViewById(R.id.tv_discovery_hot_intro);
            subscribeCountTv = (TextView) itemView.findViewById(R.id.tv_discovery_hot_subscribe_count);
            totalUpdatesTv = (TextView) itemView.findViewById(R.id.tv_discovery_hot_total_updates);
            layoutView = itemView.findViewById(R.id.layout_discovery_hot);
            subscriptionTv = (TextView) itemView.findViewById(R.id.flag1);
        }
    }
}
